package com.oto.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.stereotype.Service;

import com.oto.model.LogDO;
import com.oto.util.DateUtils;

/**
 * @author xueqiang.mi
 * @since 2010-12-19
 */
@Service
public class LogService extends BaseService {

	public LogDO get(Long id) {
		return get(LogDO.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<LogDO> lattest() {
		PersistenceManager pm = PMF.get();
		try {
			Query query = pm.newQuery(LogDO.class);
			query.setOrdering("crtDate desc");
			query.setRange(0, 30);
			Collection<LogDO> c = pm.detachCopyAll((Collection<LogDO>) query.execute());
			return new ArrayList<LogDO>(c);
		} finally {
			pm.close();
		}
	}

	public void store(int type, String content, int result) {
		LogDO obj = new LogDO();
		obj.setType(type);
		obj.setContent(content);
		obj.setResult(result);
		obj.setCrtDate(DateUtils.now());
		save(obj);
	}

	public void store(LogDO obj) {
		obj.setCrtDate(DateUtils.now());
		save(obj);
	}
}
